#ifndef FORM_HANDLE
#define FORM_HANDLE

class FormHandler : public Handler
{
public:
	FormHandler(FormView * view ,Model * model): model(model), view(view) {}
	~ FormHandler() {};

	int close()
	{    
		::DestroyWindow(handle);
		return 0;
	}

	///////////////////////////////////////////////////////////////////////////////
	// handle WM_COMMAND
	///////////////////////////////////////////////////////////////////////////////
	int command(int id, int command, LPARAM msg)
	{

		switch(id)
		{
		case IDC_BUTTON_ERASER :
			if(command == BN_CLICKED)
			{
				if (model->pBrush)
				{
					Brush * oldBrush = model->pBrush ;
					model->pBrush = new SolidBrush(static_cast<SolidBrush &>(*oldBrush));
					model->pBrush->set_color(int_RGB() ); 
					delete oldBrush ;
				}
			}
			break;
		case IDC_BUTTON_SOLID_BRUSH:
			if(command == BN_CLICKED)
			{
				if (model->pBrush)
				{
					Brush * oldBrush = model->pBrush ;
					model->pBrush = new SolidBrush(static_cast<SolidBrush &>(*oldBrush));
					delete oldBrush ;
				}
			}
			break;
		case IDC_BUTTON_GAUSSIAN_BRUSH:
			if(command == BN_CLICKED)
			{
				if (model->pBrush)
				{
					Brush * oldBrush = model->pBrush ;
					model->pBrush = new GaussianBrush(static_cast<GaussianBrush &>(*oldBrush));
					delete oldBrush ;
				}
			}
			break;
		case IDC_BUTTON_TAMU_BRUSH:
			if(command == BN_CLICKED)
			{
				if (model->pBrush)
				{

					Brush * oldBrush = model->pBrush ;
					model->pBrush = new PatternBrush(static_cast<PatternBrush &>(*oldBrush));
					model->pBrush->load_pattern("D:\\workspace\\data\\brush\\tamubrush.ppm");
					//model->pBrush->load_pattern("D:\\workspace\\data\\brush\\rabbithair.ppm");
					model->pBrush->set_transparent_color(int_RGB(255,255,255));
					delete oldBrush ;
				}
			}
			break;
		case IDC_BUTTON_LOGO_BRUSH :
			if(command == BN_CLICKED)
			{
				if (model->pBrush)
				{

					Brush * oldBrush = model->pBrush ;
					model->pBrush = new PatternBrush(static_cast<PatternBrush &>(*oldBrush));
					//model->pBrush->load_pattern("D:\\workspace\\data\\brush\\tamubrush.ppm");
					model->pBrush->load_pattern("D:\\workspace\\data\\brush\\siglogo.ppm");
					model->pBrush->set_transparent_color(int_RGB(255,255,255));
					delete oldBrush ;
				}
			}
			break;
		case IDC_BUTTON_RABBITFUR_BRUSH :
			if(command == BN_CLICKED)
			{
				if (model->pBrush)
				{

					Brush * oldBrush = model->pBrush ;
					model->pBrush = new PatternBrush(static_cast<PatternBrush &>(*oldBrush));
					//model->pBrush->load_pattern("D:\\workspace\\data\\brush\\tamubrush.ppm");
					model->pBrush->load_pattern("D:\\workspace\\data\\brush\\rabbithair.ppm");
					model->pBrush->set_transparent_color(int_RGB(0,0,0));
					delete oldBrush ;
				}
			}
			break;

		}
		//::SetFocus(

		return 0;
	}



	///////////////////////////////////////////////////////////////////////////////
	// handle WM_NOTIFY
	// The id is not guaranteed to be unique, so use NMHDR.hwndFrom and NMHDR.idFrom.
	///////////////////////////////////////////////////////////////////////////////
	int notify(int id, LPARAM lParam)
	{
		/*
		// first cast lParam to NMHDR* to know what the control is
		NMHDR* nmhdr = (NMHDR*)lParam;
		HWND from = nmhdr->hwndFrom;
		NMUPDOWN* nmUpDown = 0;

		switch(nmhdr->code)
		{
		// UpDownBox notifications =========
		case UDN_DELTAPOS:         // the change of position has begun
		// cast again lParam to NMUPDOWN*
		nmUpDown = (NMUPDOWN*)lParam;
		return view->changeUpDownPosition(from, nmUpDown->iPos + nmUpDown->iDelta);
		break;

		default:
		break;
		}

		// handled notifications
		*/
		return 0;
	}

	int  hScroll(WPARAM wParam, LPARAM lParam)
	{
		//Handle the message from the slider ;
		HWND hTrackbarRed = GetDlgItem(view->getHWND(), IDC_SLIDER_RED);
		HWND hTrackbarGreen = GetDlgItem(view->getHWND(), IDC_SLIDER_GREEN);
		HWND hTrackbarBlue = GetDlgItem(view->getHWND(), IDC_SLIDER_BLUE);

		if (hTrackbarRed == (HWND)lParam || 
			hTrackbarGreen == (HWND)lParam ||
			hTrackbarBlue == (HWND)lParam)
		{
			int_RGB color ;
			view->getBrushColor(color);
			model->pBrush->color = color;
			//view->changeBrushColor(hTrackbarRed, color);
			HWND colorBoxHandle = GetDlgItem(view->getHWND(), IDC_BUTTON_COLOR);
			SendMessage(colorBoxHandle, WM_CTLCOLORBTN, 0, (LPARAM)handle);
			return 0;
		}

		HWND hBrushSize = GetDlgItem(view->getHWND(), IDC_SLIDER_BRUSHSIZE);
		if (hBrushSize == (HWND)lParam)
		{
			model->pBrush->set_pixel_size(view->getBrushSize());
			//model->brush.update_brush_size();
			return 0; 
		}
		return 0; 
	}
	int create()
	{
		view->initControls(this->handle);
		return 0;
	}
	int paint ()
	{
		return 0;
	}
	int ctlColorEdit(HDC hDC, LPARAM lParam) 
	{
		/*
		DWORD color = lParam;
		short r = (color & 0xFF000000) >> 24;
		short g = (color & 0x00FF0000) >> 16;
		short b = (color & 0x0000FF00) >> 8;
		short a = (color & 0x000000FF);
		//SetBkColor(hDC, RGB(r,g,b));
		SetBkColor(hDC, RGB(0,255,0));
		*/
		return 0;
	}

	int ctlColorButton(HDC hDC, LPARAM lParam) 
	{
		HWND hColorButton = GetDlgItem(view->getHWND(), IDC_BUTTON_COLOR);
		HWND hSolidBrushButton = GetDlgItem(view->getHWND(), IDC_BUTTON_SOLID_BRUSH);
		HWND hGaussianBrushButton = GetDlgItem(view->getHWND(), IDC_BUTTON_GAUSSIAN_BRUSH);
		HWND hTamuBrushButton = GetDlgItem(view->getHWND(), IDC_BUTTON_TAMU_BRUSH);

		if (hColorButton == (HWND) lParam)
		{
			int_RGB color = model->pBrush->color ;
			HBRUSH brush = CreateSolidBrush( RGB(color.r, color.g, color.b));
			RECT rct;
			rct.left=0;
			rct.right=50;
			rct.top=0;
			rct.bottom=50;
			FillRect(hDC, &rct, brush);
			//HWND colorBoxHandle = GetDlgItem(view->getHWND(), IDC_BUTTON_COLOR);
			//::InvalidateRect(colorBoxHandle, & rct, false);
			//::Rectangle(hDC, 0, 0, 50, 50);
			DeleteObject(brush);
			::InvalidateRect((HWND)lParam, &rct , false);
			return 0;
		}
		else if (hSolidBrushButton == (HWND)lParam)
		{

		}
		else if (hGaussianBrushButton == (HWND)lParam)
		{
		}
		else if (hTamuBrushButton == (HWND)lParam)
		{

		}

		return 0;
	}

private:
	Model * model;                             // pointer to model component
	FormView * view;                           // pointer to view component
};


#endif